package com.intelligentmeterreadingsys.waterlist.querywaterlist.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.TypePoVo;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.db.WaterListDaoImpl;

import dbhelper.XmlNameHelper;

/**
 * 设置查询用户条件
 * 
 * @author king
 * 
 */
public class SetQueryConditionActivity extends Activity {

	public Button button1;

	public EditText edit1;// 用户编号

	public EditText edit2;// 水表编号

	public EditText edit3;// 用户姓名

	public EditText edit4;// 水表地址

	public Spinner edit5;// 表册

	public Spinner spinner;// 抄表状态

	private String waterlistid;// 表册ID

	public List<TypePoVo> map = null;

	public String[] date = { "请选择" };

	public String getWaterlistid() {
		return waterlistid;
	}

	public void setWaterlistid(String waterlistid) {
		this.waterlistid = waterlistid;
	}

	private String usertype;

	public String getUsertype() {
		return usertype;
	}

	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}

	public SharedPreferenceUtil sp;
	public QueryWaterListActivity act;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		act = (QueryWaterListActivity) getParent();
		setContentView(R.layout.waterlist_query_condition);
		sp = new SharedPreferenceUtil(this);
		inti();
	}

	public void inti() {
		edit1 = (EditText) findViewById(R.id.condition_username);
		edit2 = (EditText) findViewById(R.id.condition_usernumber);
		edit3 = (EditText) findViewById(R.id.condition_usernameinfo);
		edit4 = (EditText) findViewById(R.id.condition_waterproperties);
		edit5 = (Spinner) findViewById(R.id.condition_wateradds);
		spinner = (Spinner) findViewById(R.id.condition_copystatus);

		edit1.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					edit2.setText("");
					edit3.setText("");
					edit4.setText("");
					edit5.setSelection(0, true);
					spinner.setSelection(0, true);
				}

			}
		});
		edit2.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					edit1.setText("");
					edit3.setText("");
					edit4.setText("");
					edit5.setSelection(0, true);
					spinner.setSelection(0, true);
				}

			}
		});

		button1 = (Button) findViewById(R.id.condition_query_button);
		WaterListDaoImpl dao = new WaterListDaoImpl(this);
		map = dao.queryWaterList();
		dao.close();

		List<String> listss = new ArrayList<String>();
		listss.add("请选择");
		if (map != null) {
			for (int i = 0; i < map.size(); i++) {
				System.out.println(map.get(i).getName());
				listss.add(map.get(i).getName());
			}
		}

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				usertype = arg0.getItemAtPosition(arg2).toString();

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				usertype = "未抄";

			}

		});

		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

		ArrayAdapter<String> ada1 = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listss);// 放数据
		ada1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置打开方式
		edit5.setAdapter(ada1);
		edit5.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				if (arg0.getItemAtPosition(arg2).toString().equals("请选择")) {
					waterlistid = "0";
				} else {
					waterlistid = waterlistId(arg0.getItemAtPosition(arg2)
							.toString());

				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				waterlistid = "0";

			}
		});
	}

	@Override
	protected void onResume() {
		edit1.setText(sp.getValue(XmlNameHelper.QUERYUSERNUMBER));
		edit2.setText(sp.getValue(XmlNameHelper.QUERYWATERNUMBER));
		edit4.setText(sp.getValue(XmlNameHelper.QUERYWATERADDS));
		edit3.setText(sp.getValue(XmlNameHelper.QUERYUSERNAME));
		String[] da = new String[] { "请选择", "未抄", "已抄", "二次抄表" };
		for (int i = 0; i < da.length; i++) {
			if (sp.getValue(XmlNameHelper.QUERYCOPYWATERSTATUS).equals(da[i])) {
				spinner.setSelection(i, true);
			}
		}
		if (map != null) {
			for (int i = 0; i < map.size(); i++) {
				if (sp.getValue(XmlNameHelper.QUERYWATERLIST).equals(
						map.get(i).getNumber())) {
					edit5.setSelection(i + 1, true);
					return;
				}
			}
		}
		super.onResume();
	}

	// 通过表册名得到ID
	public String waterlistId(String name) {
		String id = "";
		for (int i = 0; i < map.size(); i++) {
			if (map.get(i).getName().equals(name)) {
				id = map.get(i).getNumber();
				return id;
			}
		}
		return id;

	}
}
